package com.shengqi.xinnongren.work.ui.home.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.shengqi.xinnongren.R;

import java.util.List;

public class ChooseSubClassAdapter extends RecyclerView.Adapter<ChooseSubClassAdapter.Holder> {

    private List<String> mStringList;
    private Context mContext;
    private int mDefaultColor = Color.parseColor("#666666");
    private int mChoseColor;
    private int mCurChosePosition = -1;

    private OnItemClickListener mOnItemClickListener;

    public ChooseSubClassAdapter(List<String> stringList, Context context) {
        mStringList = stringList;
        mContext = context;
        mChoseColor = ContextCompat.getColor(context, R.color.colorPrimary);
    }


    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View layout = LayoutInflater.from(mContext).inflate(R.layout.item_class_choose, parent, false);
        return new Holder(layout);
    }

    @Override
    public void onBindViewHolder(@NonNull final Holder holder, final int position) {
        if (position > -1) {
            if (position == mCurChosePosition) {
                holder.mTvTitle.setTextColor(mChoseColor);
                holder.itemView.setBackgroundColor(Color.WHITE);
                holder.mMark.setVisibility(View.VISIBLE);
            } else {
                holder.itemView.setBackgroundColor(Color.TRANSPARENT);
                holder.mTvTitle.setTextColor(mDefaultColor);
                holder.mMark.setVisibility(View.GONE);
            }
        }
        holder.mTvTitle.setText(mStringList.get(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCurChosePosition = holder.getAdapterPosition();
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(position);
                }
                notifyDataSetChanged();
            }
        });
    }

    public void setCurChosePosition(int position) {
        if (position >= getItemCount())
            throw new RuntimeException("老铁，下标越界了");
        mCurChosePosition = position;
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick(mCurChosePosition);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mStringList.size();
    }

    class Holder extends RecyclerView.ViewHolder {
        TextView mTvTitle;
        View mMark;

        Holder(View itemView) {
            super(itemView);
            mTvTitle = itemView.findViewById(R.id.tv_title);
            mMark = itemView.findViewById(R.id.v_mark);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }
}